<?php
/**
 * @package YARBB
 * @copyright Авторские права (C) 2009 www.yar-it.com. Все права защищены.
 * @license Лицензия http://www.gnu.org/licenses/gpl-2.0.htm GNU/GPL, или help/license.php
 * YARBB! - свободное программное обеспечение распространяемое по условиям лицензии GNU/GPL
 * Для получения информации о используемых расширениях и замечаний об авторском праве, смотрите файл copyright.php.
 */

defined( '_VALID_MOS' ) or die();

$mainframe->setPageTitle( yarbb_htmlspecialchars($yarbb_config['o_board_title']) );

define('YARBB_ALLOW_INDEX', 1);

// подфорумы >
global $database;
$sql = 'SELECT f.num_topics, f.num_posts, f.parent_forum_id, f.last_post_id, f.last_poster, f.last_post, f.id, f.forum_name, p.poster_id
	as last_poster_id, t.subject
	FROM #__forum_forums AS f
	LEFT JOIN #__forum_posts AS p ON (p.id=f.last_post_id)
	LEFT JOIN #__forum_topics AS t ON t.last_post_id=f.last_post_id
	WHERE f.parent_forum_id<>0
	ORDER BY disp_position';
$database->setQuery($sql);
$sub_forum = $database->loadAssocList();
$sfcount = 0;
foreach($sub_forum as $forum) {
	$sfdb[$sfcount][0] = $forum['parent_forum_id'];
	$sfdb[$sfcount][1] = $forum['num_topics'];
	$sfdb[$sfcount][2] = $forum['num_posts'];
	$sfdb[$sfcount][3] = $forum['last_post_id'];
	$sfdb[$sfcount][4] = $forum['last_poster'];
	$sfdb[$sfcount][5] = $forum['last_post'];
	$sfdb[$sfcount][6] = $forum['id'];
	$sfdb[$sfcount][7] = $forum['forum_name'];
	$sfdb[$sfcount][8] = $forum['last_poster_id'];
	$sfdb[$sfcount][9] = $forum['subject'];
	$sfcount++;
}
unset($sub_forum);
// подфорумы <

// вывод главных категорий и форумов >

$sql = 'SELECT c.id AS cid, c.cat_name, f.id AS fid, f.forum_name, f.forum_desc, f.redirect_url, f.moderators, f.num_topics, f.num_posts, f.last_post, f.last_post_id, f.last_poster, f.parent_forum_id, p.poster_id as last_poster_id, t.subject
	FROM #__forum_categories AS c
	INNER JOIN #__forum_forums AS f ON c.id=f.cat_id
	LEFT JOIN #__forum_topics AS t ON t.last_post_id=f.last_post_id
	LEFT JOIN #__forum_forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$yarbb_user['g_id'].')
	LEFT JOIN #__forum_posts AS p ON (p.id=f.last_post_id)
	WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND (f.parent_forum_id IS NULL OR f.parent_forum_id=0)
	ORDER BY c.disp_position, c.id, f.disp_position';
$main_forum = $database->setQuery($sql)->loadAssocList();

$cat_count = 0;
$cur_category = 0;
foreach($main_forum as $cur_forum) {
	$moderators = '';

	// вывод родительской категории
	if ($cur_forum['cid'] != $cur_category) {
		if ($cur_category != 0) {
			echo "\t\t\t</tbody>\n\t\t\t</table>\n\t\t</div></div></div>\n\n";
		}
		++$cat_count;

		?>
<div id="idx<?php echo $cat_count ?>" class="blocktable">
	<h2><span class="brdcolor2"><?php echo $cur_forum['cat_name'] ?></span></h2>
	<div class="box" id="box_<?php echo $cat_count ?>">
		<div class="inbox">
			<table width="100%" cellspacing="0">
				<thead>
					<tr>
						<th class="tcl" scope="col"><?php echo _Y_FORUMS; ?></th>
						<th class="tc2" scope="col"><?php echo _Y_TOPICS; ?></th>
						<th class="tc3" scope="col"><?php echo _Y_REPLYSY; ?></th>
						<th class="ag_tcr" scope="col"><?php echo _Y_LASTPOST; ?></th>
					</tr>
				</thead>
				<tbody>
							<?php
							$cur_category = $cur_forum['cid'];
						}
						$item_status = '';
						$icon_type = 'icon';

						// отмечаем новые сообщения
						if (!$yarbb_user['is_guest'] && $cur_forum['last_post'] > $yarbb_user['last_visit']) {
							$item_status = 'inew';
							$icon_type = 'icon inew';
						}

						// Is this a redirect forum?
						if ($cur_forum['redirect_url'] != '') {
							$forum_field = '<h3><a href="'.sefRelToAbs(yarbb_htmlspecialchars($cur_forum['redirect_url'])).'" title="'._Y_LINKTO.' '.yarbb_htmlspecialchars($cur_forum['redirect_url']).'">'.yarbb_htmlspecialchars($cur_forum['forum_name']).'</a></h3>';
							$num_topics = $num_posts = '&nbsp;';
							$item_status = 'iredirect';
							$icon_type = 'icon';
						}

						$forum_field = '<h3><a href="'.sefRelToAbs(YARBB_URL.'&amp;task=viewforum&amp;id='.$cur_forum['fid']).':'._translit($cur_forum['forum_name']).'" title="'.yarbb_htmlspecialchars($cur_forum['forum_name']).'">'.yarbb_htmlspecialchars($cur_forum['forum_name']).'</a><br /></h3>';


						// вывод подфорумов >
						$n_t = 0;
						$n_p = 0;
						$l_pid = $cur_forum['last_post_id'];
						$l_pr = $cur_forum['last_poster'];
						$l_pr_id = $cur_forum['last_poster_id'];
						$l_pr_su = $cur_forum['subject'];
						$l_post = $cur_forum['last_post'];
						for ($i = 0; $i < $sfcount; $i++) {
							if ($sfdb[$i][0] == $cur_forum['fid']) {
								$n_t = $n_t + $sfdb[$i][1];
								$n_p = $n_p + $sfdb[$i][2];
								if ($l_pid < $sfdb[$i][3]) {
									$l_pid = $sfdb[$i][3];
									$l_pr = $sfdb[$i][4];
									$l_pr_id = $sfdb[$i][8];
									$l_post = $sfdb[$i][5];
									$l_pr_su = $sfdb[$i][9];
								}
							}
						}
						$num_topics = $n_t + $cur_forum['num_topics'];
						$num_posts = $n_p + $cur_forum['num_posts'];
						// вывод подфорумов <

						if ($cur_forum['forum_desc'] != '')
							$forum_field .= "\n\t\t\t\t\t\t\t\t".$cur_forum['forum_desc'];

						// вывод вывод последних тем >
						$idm = $l_pr_su;
						// обрезаем слишком длинное
						if(strlen($idm) > 25) {
							$idmComp = str_replace('"', "''", $idm);
							$idm = Jstring :: substr($idm, 0, 25).'...';
						} else {
							$idmComp = '';
						}
						// последняя тема/сообщение
						if ($l_post != '') {
							$idmT = (isset($idmComp)) ? ' title="'.$idmComp.'"' : '';
							$last_post = 'Re: <a href="'.sefRelToAbs(YARBB_URL.'&amp;task=viewtopic&amp;pid='.$l_pid.'#p'.$l_pid).'"'.$idmT.'>'.$idm.'</a>
		<br />'.format_time($l_post).'<br />
		<span class="byuser">'._Y_YARBY.':&nbsp;
			<a href="'.sefRelToAbs('index.php?option=com_users&task=profile&amp;user='.$l_pr_id).':'._translit($l_pr).'" title="'._Y_LOOKPROF.'">'.yarbb_htmlspecialchars($l_pr).'</a>
		</span>';
						}else {
							$last_post = '&nbsp;';
						}
						// вывод вывод последних тем <

						// формирование списка модераторов >
						if ($cur_forum['moderators'] != '') {
							$mods_array = unserialize($cur_forum['moderators']);
							$moderators = array();
							while (list($mod_username, $mod_id) = @each($mods_array)) {
								$moderators[] = '<a href="'.sefRelToAbs('index.php?option=com_users&task=profile&amp;user='.$mod_id).'">'.yarbb_htmlspecialchars($mod_username).'</a>';
							}
							$moderators = "\t\t\t\t\t\t\t\t".'<p><em>('._Y_MODERATORS.'</em> '.implode(', ', $moderators).')</p>'."\n<br />";
						}
						// формирование списка модераторов <
						?>
					<tr id="forum_f<?php echo $cur_forum['fid'] ?>"<?php if ($item_status != '') echo ' class="'.$item_status.'"'; ?>>
						<td class="tcl"><div class="<?php echo $icon_type ?>"></div>
							<div class="moder_list"><?php echo $forum_field."\n".$moderators ?>
									<?php
									$sub_forums_list = array();
									if(!empty($sfdb)) {
										foreach ($sfdb as $sub_forums) {
											if($cur_forum['fid'] == $sub_forums[0]) {
												$sub_forums_list[] = '<a class="subforum_name" href="'.sefRelToAbs(YARBB_URL.'&amp;task=viewforum&amp;id='.$sub_forums[6]).':'._translit($sub_forums[7]).'">'.yarbb_htmlspecialchars($sub_forums[7]).'</a>';
											}
										}
										if(!empty($sub_forums_list)) {
											$sub_forums_list = "\t\t\t\t\t\t\t\t".'<span class="subforum">'._Y_PARENTFORUM.':</span> '.implode(', ', $sub_forums_list)."\n";
											if ($cur_forum['forum_desc'] != NULL) {
												echo '<br />';
											}
											echo $sub_forums_list;
										}
									}
									?>
							</div></td>
						<td class="tc2"><?php echo $num_topics; // число тем ?></td>
						<td class="tc3"><?php echo $num_posts; // ичсло сообщений ?></td>
						<td class="ag_tcr"><?php echo $last_post; // последнее сообщение ?></td>
					</tr>
						<?php
					}
					unset($main_forum,$cur_forum,$sfdb,$sub_forums_list);
// завершаем вывод
					if ($cur_category > 0) {
						echo "\t\t\t</tbody>\n\t\t\t</table>\n\t\t</div>\n\t</div>\n</div>\n\n";
					}else {
						echo '<div id="idx0" class="block"><div class="box"><div class="inbox"><p>'._Y_BANNONE.'</p></div></div></div>';
					}

					$footer_style = 'index';
